﻿Imports System.Drawing.Image
Imports System.IO

Public Class frmAdd

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'InventoryDataSet.items' table. You can move, or remove it, as needed.
        'Me.ItemsTableAdapter.Fill(Me.InventoryDataSet.items)


        'Clear all fields upon opening the new form by using the below code
        Me.ItemsBindingSource.AddNew()

    End Sub

    Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click

        'The name textbox is the only required field other then the PK which is not shown
        If ItemNameTextBox.Text = "" Then
            MessageBox.Show("You Must Enter A Valid Item Name", "Enigma Home Inventory", _
                        MessageBoxButtons.OK, MessageBoxIcon.Error)

        Else

            Try
                Me.Validate()
                Me.ItemsBindingSource.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.InventoryDataSet)

                'Make sure that frmMain gets updated when closed
                frmMain.ItemsTableAdapter.Fill(frmMain.InventoryDataSet.items)

                'close frmAdd and show frmMain
                Me.Close()
                frmMain.Show()
            Catch ex As Exception

                MsgBox(ex.Message)

            End Try

        End If

    End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddImage.Click

    Me.OpenFileDialog1.FileName = Nothing

    Me.OpenFileDialog1.ShowDialog()

    If Not Me.OpenFileDialog1.FileName = Nothing Then
        Me.PictureBox1.ImageLocation = Me.OpenFileDialog1.FileName
    End If

End Sub

    ' Image Conversion removed this as it is old code.

    'Public Function convertImage(ByVal myImage As Image) As Byte()
    '    Dim mstream As New MemoryStream()
    '    myImage.Save(mstream, System.Drawing.Imaging.ImageFormat.Jpeg)

    '    Dim myBytes(mstream.Length - 1) As Byte
    '    mstream.Position = 0

    '    mstream.Read(myBytes, 0, mstream.Length)

    '    Return myBytes

    'End Function

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    'close frmAdd and show frmMain
    Me.Close()
    frmMain.Show()

End Sub

#Region " Windows Form Designer generated code "

'disables close
Protected Overrides ReadOnly Property CreateParams() As CreateParams
    Get
        Dim cp As CreateParams = MyBase.CreateParams
        Const CS_NOCLOSE As Integer = &H200
        cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
        Return cp
    End Get
End Property
#End Region
End Class